//
//  TeamStarDetailViewController.m
//  Doctor1.0
//
//  Created by mk on 15/7/10.
//  Copyright (c) 2015年 cocoa-mk. All rights reserved.
//

#import "TeamStarDetailViewController.h"
#import "DetailTableViewCell.h"
#import "LeaderTableViewCell.h"
#import "Members.h"
#import "TeamLeader.h"
#import "StarViewController.h"
#import "Doctor.h"
#import "NewReservationListViewController.h"

@interface TeamStarDetailViewController ()<UITableViewDataSource,UITableViewDelegate,UIWebViewDelegate
>
{
   NSMutableDictionary * dicTeamData;
   TeamLeaderInfo * leader;
   MembersList * memberData;
}


@end

@implementation TeamStarDetailViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
  
    memberData = [[MembersList alloc]init];
    leader = [[TeamLeaderInfo alloc]init];
    dicTeamData = [[NSMutableDictionary alloc]initWithCapacity:0];
    titleLabel.text = self.strTitle;
    btnyuyue.hidden = YES;
    [self getData];
}


- (void)getData
{
    SetHUDShowText(@"加载中..")
    NSString * strUrl = [NSString stringWithFormat:@"%@",self.Url];
    
    memberData.urlPath = strUrl;
    memberData.strDataKey = @"members";
    
    [memberData getLoadData:^(AFHTTPRequestOperation *operation, id responseObject)
    {
        NSDictionary * dicData = [responseObject objectForKey:requestDataKey];
        //NSLog(@"==%@",responseObject);
        dicTeamData = [dicData objectForKey:@"team"];
        //领导人
        [leader parseData:[dicData objectForKey:@"leader"]];
        [tableMain reloadData];
        labHosName.text = [dicTeamData objectForKey:@"teamName"];

//        if ([dicTeamData objectForKey:@"isContracted"])
//        {
//          btnyuyue.hidden = ([NullObjToString([dicTeamData objectForKey:@"isContracted"]) isEqualToString:@"0"]);
//        }else
//        {
//           btnyuyue.hidden = YES;
//        }
        
         btnyuyue.hidden = NO;
        
         SetHUDHidden;
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error)
     {
         SetHUDHidden;
         HUDShowRequestError(error.domain, self.view)
    }];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return memberData.dataArray.count + (leader.leader_id.length>0?1:0);
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0)
    {
        NSString * CellIdentifier = @"LeaderTableViewCellStr";
        LeaderTableViewCell *cell = (LeaderTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if(!cell)
        {
            cell = (LeaderTableViewCell *)[[[NSBundle mainBundle] loadNibNamed:@"LeaderTableViewCell" owner:self options:nil] objectAtIndex:0];
        }
        [cell fillDataWith:leader noMember:(memberData.dataArray.count==0?YES:NO) teamData:dicTeamData];
       
        //更新UIView的layout过程和Autolayout
        [cell layoutIfNeeded];
        [cell updateConstraintsIfNeeded];
        
        //通过systemLayoutSizeFittingSize返回最低高度
        CGFloat height = [cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
        
        return height;

    }
    return 111;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0)
    {
        NSString * CellIdentifier = @"LeaderTableViewCellStr";
        LeaderTableViewCell *cell=(LeaderTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if(!cell)
        {
            cell = (LeaderTableViewCell *)[[[NSBundle mainBundle] loadNibNamed:@"LeaderTableViewCell" owner:self options:nil] objectAtIndex:0];
        }
        [cell fillDataWith:leader noMember:(memberData.dataArray.count==0?YES:NO) teamData:dicTeamData];
        return cell;
    }
    
    NSString * CellIdentifier = @"star";
    DetailTableViewCell *cell=(DetailTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if(!cell)
    {
        cell = (DetailTableViewCell *)[[[NSBundle mainBundle] loadNibNamed:@"DetailTableViewCell" owner:self options:nil] objectAtIndex:0];
    }
    
     [cell fillDataWith:[memberData.dataArray objectAtIndex:indexPath.row-1]];
   
    return cell;
}




#pragma mark 预约团队
- (IBAction)btnAttpAction:(id)sender
{
    if (![gProjConfig needLogin])
    {
        Doctor * info = [[Doctor alloc]init];
       
        info.ID = [dicTeamData objectForKey:@"teamId"];//id
        info.name = [dicTeamData objectForKey:@"teamName"];//名字
        info.hpName= leader.leader_hpName;//医院
        info.hpDeptName= leader.leader_hpDeptName;//科室名字
        info.actionUrl= [dicTeamData objectForKey:@"actionUrl"];//url
    
        NewReservationListViewController * list = [[NewReservationListViewController alloc]initWithNibName:@"NewReservationListViewController" bundle:nil];
        list.doctorData = info;
        list.reservationDoctor = NO;
        [self.navigationController pushViewController:list animated:YES];
    }
}



- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
